﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;
using KeaoMDS.SysManage;
using KeaoMDS.Auth;
using KeaoMDS.InfoManage;
using KeaoMDS.Weight;
using KeaoMDS.Report;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using KeaoUpLoad;
using GoldPrinter;
using System.Xml;
using System.Reflection;
using KeaoMDS.AppCode;
namespace KeaoMDS
{
    public partial class MainForm : BaseForm
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public static void RiZhi(string RiZhiContent)
        {
            TableRow riZhiRow = new TableRow("tbl_rizhi", Guid.NewGuid().ToString(), DBCtl.插入);
            riZhiRow.DataCols.Add(new TableCol() { ColName = "rizhineirong", ValueMember = RiZhiContent, ValueType = DataValueCommonType.字符串 });
            riZhiRow.DataCols.Add(new TableCol() { ColName = "rizhishijian", ValueMember = DateTime.Now, ValueType = DataValueCommonType.日期时间 });
            riZhiRow.DataCols.Add(new TableCol() { ColName = "caozuoyuan", ValueMember = KeaoUtils._CurrentUserRow["yonghuxingming"], ValueType = DataValueCommonType.字符串 });
            riZhiRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
            riZhiRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
            riZhiRow.DataCols.Add(new TableCol() { ColName = "bangfang", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "磅房"), ValueType = DataValueCommonType.字符串 });
            riZhiRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
            KeaoUtils._DB.SaveTableRows(riZhiRow);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (KeaoUtils.ShowQuestionMessage("确定要退出系统吗？") == DialogResult.Yes)
            {
                if (m_ThreadSocket != null)
                {
                    m_ThreadSocket.Abort();
                }
                if (m_DTU != null)
                {
                    m_DTU.CloseDevice();
                }
                if (m_Listener != null)
                {
                    m_Listener.CloseDevice();
                }
                Environment.Exit(0);
            }
            else
            {
                e.Cancel = true;
                return;
            }
        }

        private BaseUpLoad m_DTU;
        private Thread m_ThreadSocket;
        private string m_MsgSendDate = string.Empty;
        public static BaseListener m_Listener = null;
        
        private DataTable m_ClientDt = new DataTable();//客户端信息

        private void MainForm_Load(object sender, EventArgs e)
        {
            VersionUpdate versionUpdate = new VersionUpdate();
            versionUpdate.UpdateVersion();

            m_ClientDt.Columns.Add("kehubianhao");
            m_ClientDt.Columns.Add("zhandian");
            m_ClientDt.Columns.Add("bangfang");
            DataRow newRow = m_ClientDt.NewRow();
            newRow["kehubianhao"] = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号");
            newRow["zhandian"] = KeaoUtils.ConfigGetNodeAttr("客户", "站点");
            newRow["bangfang"] = KeaoUtils.ConfigGetNodeAttr("客户", "磅房");
            m_ClientDt.Rows.Add(newRow);

            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
            dataGridView3.AutoGenerateColumns = false;
            dataGridView7.AutoGenerateColumns = false;
            dataGridView8.AutoGenerateColumns = false;

            status_lb_currentUser.Text = string.Format("{0}", KeaoUtils._CurrentUserRow["yonghuxingming"]);
            this.WindowState = FormWindowState.Maximized;

            BindPiZhongChaoCha();
            BindChaoShi();
            BindCheLiangYunShuCiShu();
            BindRiJieRuKu();
            BindRiJieChuKu();

            //ThreadPool.QueueUserWorkItem(new WaitCallback(Bak));
            Thread t = new Thread(new ParameterizedThreadStart(Bak));
            t.Start();

            if (KeaoUtils.ConfigGetNodeAttr("网络设备", "状态") == "启用")
            {
                m_DTU = Assembly.LoadFrom("KeaoUpLoad.dll").CreateInstance(string.Format("KeaoUpLoad.{0}", KeaoUtils.ConfigGetNodeAttr("网络设备", "类型"))) as BaseUpLoad;
                m_DTU.InitNetWorkStatus += new EventHandler(m_Socket_InitNetWorkStatus);
                m_DTU.OnReceiveData += new OnReceiveDataHandler(m_DTU_OnReceiveData);
                m_ThreadSocket = new Thread(new ThreadStart(UpLoadData));
                m_ThreadSocket.Start();
            }
            else
            {
                status_lb_linkserver.Text = "设备禁用！";
            }

            if (KeaoUtils.ConfigGetNodeAttr("遥控监测设备", "状态") == "启用")
            {
                m_Listener = Assembly.LoadFrom("KeaoListener.dll").CreateInstance(string.Format("KeaoListener.{0}", KeaoUtils.ConfigGetNodeAttr("遥控监测设备", "类型"))) as BaseListener;
                if (m_Listener.ConnectDevice(KeaoUtils.ConfigGetNodeAttr("遥控监测设备", "地址"), int.Parse(KeaoUtils.ConfigGetNodeAttr("遥控监测设备", "波特率")), KeaoUtils.Decrypt(KeaoUtils.ConfigGetNodeAttr("遥控监测设备", "间隔标识")), KeaoUtils.ConfigGetNodeAttr("遥控监测设备", "心跳包")))
                {
                    status_lb_yaokong.Text = "连接成功。";
                    m_Listener.OnDisConnectDevice += new EventHandler(m_Listener_OnDisConnectDevice);
                }
                else
                {
                    status_lb_yaokong.Text = "连接失败！";
                }
            }
            else
            {
                status_lb_yaokong.Text = "设备禁用！";
            }
        }

        void m_Listener_OnDisConnectDevice(object sender, EventArgs e)
        {
           //当设备断开连接
            status_lb_yaokong.Text = "设备断开连接！";
        }

        void m_DTU_OnReceiveData(object sender, DataInfoEventArgs e)
        {
            //收到数据
            try
            {
                DataInfo currentDataInfo = e.m_DataInfo;
                Newtonsoft.Json.Linq.JArray dt = Newtonsoft.Json.JsonConvert.DeserializeObject(currentDataInfo.CurrentJson) as Newtonsoft.Json.Linq.JArray;
                switch (currentDataInfo.CurrentByte)
                {
                    #region 处理回发编号
                    case 0x21:
                        TableRow yongHuRow = new TableRow() { TableName = "tbl_yonghu", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        yongHuRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(yongHuRow);
                        break;
                    case 0x22:
                        TableRow jueSeRow = new TableRow() { TableName = "tbl_juese", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        jueSeRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(jueSeRow);
                        break;
                    case 0x23:
                        TableRow wuLiaoRow = new TableRow() { TableName = "tbl_wuliao", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        wuLiaoRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(wuLiaoRow);
                        break;
                    case 0x24:
                        TableRow xingHaoRow = new TableRow() { TableName = "tbl_xinghao", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(xingHaoRow);
                        break;
                    case 0x25:
                        TableRow danWeiRow = new TableRow() { TableName = "tbl_danwei", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        danWeiRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(danWeiRow);
                        break;
                    case 0x26:
                        TableRow biaoDuanRow = new TableRow() { TableName = "tbl_biaoduan", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(biaoDuanRow);
                        break;
                    case 0x27:
                        TableRow yunShuRow = new TableRow() { TableName = "tbl_yunshugongsi", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        yunShuRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(yunShuRow);
                        break;
                    case 0x28:
                        TableRow jianCeRow = new TableRow() { TableName = "tbl_yaokongjiance", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        jianCeRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(jianCeRow);
                        break;
                    case 0x29:
                        TableRow yiChangRow = new TableRow() { TableName = "tbl_pizhongyichang", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        yiChangRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(yiChangRow);
                        break;
                    case 0x30:
                        TableRow chaoShiRow = new TableRow() { TableName = "tbl_chaoshi", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        chaoShiRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(chaoShiRow);
                        break;
                    case 0x31:
                        TableRow chengZhongRow = new TableRow() { TableName = "tbl_chengzhong", ID = string.Format("{0}",dt[0]["id"]), Ctl = DBCtl.修改 };
                        chengZhongRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(chengZhongRow);
                        break;
                    case 0x34:
                        TableRow riZhiRow = new TableRow() { TableName = "tbl_rizhi", ID = string.Format("{0}", dt[0]["id"]), Ctl = DBCtl.修改 };
                        riZhiRow.DataCols.Add(new TableCol() { ColName="shangchuanzhuangtai", ValueMember="1", ValueType= DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(riZhiRow);
                        break;
                    case 0x35:
                        TableRow tuiKuRow = new TableRow() { TableName="tbl_tuiku",ID=string.Format("{0}",dt[0]["id"]), Ctl= DBCtl.修改 };
                        tuiKuRow.DataCols.Add(new TableCol() { ColName="shangchuanzhuangtai", ValueMember="1", ValueType= DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(tuiKuRow);
                        break;
                    case 0x38:
                        TableRow gongYingShangRow = new TableRow() {TableName="tbl_gongyingshang",ID=string.Format("{0}",dt[0]["id"]),Ctl= DBCtl.修改 };
                        gongYingShangRow.DataCols.Add(new TableCol() { ColName="shangchuanzhuangtai", ValueMember="1", ValueType= DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(gongYingShangRow);
                        break;
                    case 0x39:
                        TableRow heTongRow = new TableRow() { TableName="tbl_hetong",ID =string.Format("{0}",dt[0]["id"]),Ctl= DBCtl.修改 };
                        heTongRow.DataCols.Add(new TableCol() { ColName="shangchuanzhuangtai", ValueMember="1", ValueType= DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(heTongRow);
                        break;
                    case 0x40:
                        TableRow peiBiRow = new TableRow() {TableName="tbl_peihebi", ID=string.Format("{0}",dt[0]["id"]),Ctl= DBCtl.修改 };
                        peiBiRow.DataCols.Add(new TableCol() { ColName="shangchuanzhuangtai", ValueMember="1", ValueType= DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(peiBiRow);
                        break;
                    case 0xa1:
                        TableRow peiBiDanRow = new TableRow() { TableName="tbl_peibidan",ID=string.Format("{0}",dt[0]["id"]),Ctl= DBCtl.修改 };
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName="shangchuanzhuangtai", ValueMember="1", ValueType= DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(peiBiDanRow);
                        break;
                    case 0xa2:
                        TableRow ziDianRow = new TableRow() { TableName="tbl_zidian",ID=string.Format("{0}",dt[0]["id"]), Ctl= DBCtl.修改 };
                        ziDianRow.DataCols.Add(new TableCol() { ColName="shangchuanzhuangtai", ValueMember="1", ValueType= DataValueCommonType.字符串 });
                        KeaoUtils._DB.SaveTableRows(ziDianRow);
                        break;
                    #endregion
                }
                switch (currentDataInfo.CurrentByte)
                {
                    #region 处理表数据
                    case 0x01:
                        DataTable yongHuDt = KeaoUtils._DB.GetDataTableByCondition("tbl_yonghu", "id", null, KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", dt[0]["id"]), "", SearchConditionType.等于), null, null);
                        TableRow yongHuRow = new TableRow() { TableName = "tbl_yonghu", ID = string.Format("{0}", dt[0]["id"]) };
                        if (yongHuDt.Rows.Count == 0)
                        {
                            yongHuRow.Ctl = DBCtl.插入;
                        }
                        else
                        {
                            yongHuRow.Ctl = DBCtl.修改;
                        }
                        yongHuRow.DataCols.Add(new TableCol() { ColName = "yonghuxingming", ValueMember = string.Format("{0}", dt[0]["yonghuxingming"]), ValueType = DataValueCommonType.字符串 });
                        yongHuRow.DataCols.Add(new TableCol() { ColName = "yonghudengluming", ValueMember = string.Format("{0}", dt[0]["yonghudengluming"]), ValueType = DataValueCommonType.字符串 });
                        yongHuRow.DataCols.Add(new TableCol() { ColName = "yonghumima", ValueMember = string.Format("{0}", dt[0]["yonghumima"]), ValueType = DataValueCommonType.字符串 });
                        yongHuRow.DataCols.Add(new TableCol() { ColName = "yonghujuese", ValueMember = string.Format("{0}", dt[0]["yonghujuese"]), ValueType = DataValueCommonType.字符串 });
                        yongHuRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        yongHuRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = string.Format("{0}", dt[0]["zhandian"]), ValueType = DataValueCommonType.字符串 });
                        yongHuRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = string.Format("{0}", dt[0]["kehubianhao"]), ValueType = DataValueCommonType.字符串 });
                        if (KeaoUtils._DB.SaveTableRows(yongHuRow) > 0)
                        {
                            
                        }
                        break;
                    case 0x02:
                        DataTable jueSeDt = KeaoUtils._DB.GetDataTableByCondition("tbl_juese", "id", null, KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", dt[0]["id"]), "", SearchConditionType.等于), null, null);
                        TableRow jueSeRow = new TableRow() { TableName = "tbl_juese", ID = string.Format("{0}", dt[0]["id"]) };
                        if (jueSeDt.Rows.Count == 0)
                        {
                            jueSeRow.Ctl = DBCtl.插入;
                        }
                        else
                        {
                            jueSeRow.Ctl = DBCtl.修改;
                        }
                        jueSeRow.DataCols.Add(new TableCol() { ColName = "juesemingcheng", ValueMember = string.Format("{0}", dt[0]["juesemingcheng"]), ValueType = DataValueCommonType.字符串 });
                        jueSeRow.DataCols.Add(new TableCol() { ColName = "juesequanxian", ValueMember = string.Format("{0}", dt[0]["juesequanxian"]), ValueType = DataValueCommonType.字符串 });
                        jueSeRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        jueSeRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = string.Format("{0}", dt[0]["zhandian"]), ValueType = DataValueCommonType.字符串 });
                        jueSeRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = string.Format("{0}", dt[0]["kehubianhao"]), ValueType = DataValueCommonType.字符串 });
                        if (KeaoUtils._DB.SaveTableRows(jueSeRow) > 0)
                        {

                        }
                        break;
                    case 0x03:
                        DataTable wuLiaoDt = KeaoUtils._DB.GetDataTableByCondition("tbl_wuliao", "id", null, KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", dt[0]["id"]), "", SearchConditionType.等于), null, null);
                        TableRow wuLiaoRow = new TableRow() { TableName = "tbl_wuliao", ID = string.Format("{0}", dt[0]["id"]) };
                        if (wuLiaoDt.Rows.Count == 0)
                        {
                            wuLiaoRow.Ctl = DBCtl.插入;
                        }
                        else
                        {
                            wuLiaoRow.Ctl = DBCtl.修改;
                        }
                        wuLiaoRow.DataCols.Add(new TableCol() { ColName = "wuliaomingcheng", ValueMember = string.Format("{0}", dt[0]["wuliaomingcheng"]), ValueType = DataValueCommonType.字符串 });
                        wuLiaoRow.DataCols.Add(new TableCol() { ColName = "wuliaoleixing", ValueMember = string.Format("{0}", dt[0]["wuliaoleixing"]), ValueType = DataValueCommonType.字符串 });
                        wuLiaoRow.DataCols.Add(new TableCol() { ColName = "wuliaozhuangtai", ValueMember = string.Format("{0}", dt[0]["wuliaozhuangtai"]), ValueType = DataValueCommonType.字符串 });
                        wuLiaoRow.DataCols.Add(new TableCol() { ColName = "shezhiquxiang", ValueMember = string.Format("{0}", dt[0]["shezhiquxiang"]), ValueType = DataValueCommonType.字符串 });
                        wuLiaoRow.DataCols.Add(new TableCol() { ColName = "shezhifahuozhongliang", ValueMember = string.Format("{0}", dt[0]["shezhifahuozhongliang"]), ValueType = DataValueCommonType.字符串 });
                        wuLiaoRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember ="1", ValueType = DataValueCommonType.字符串 });
                        wuLiaoRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = string.Format("{0}", dt[0]["zhandian"]), ValueType = DataValueCommonType.字符串 });
                        wuLiaoRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = string.Format("{0}", dt[0]["kehubianhao"]), ValueType = DataValueCommonType.字符串 });
                        if (KeaoUtils._DB.SaveTableRows(wuLiaoRow) > 0)
                        {
                            
                        }
                        break;
                    case 0x04:
                        DataTable xingHaoDt = KeaoUtils._DB.GetDataTableByCondition("tbl_xinghao", "id", null, KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", dt[0]["id"]), "", SearchConditionType.等于), null, null);
                        TableRow xingHaoRow = new TableRow() { TableName = "tbl_xinghao", ID = string.Format("{0}", dt[0]["id"]) };
                        if (xingHaoDt.Rows.Count == 0)
                        {
                            xingHaoRow.Ctl = DBCtl.插入;
                        }
                        else
                        {
                            xingHaoRow.Ctl = DBCtl.修改;
                        }
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "xinghao", ValueMember = string.Format("{0}", dt[0]["xinghao"]), ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "wuliaobianhao", ValueMember = string.Format("{0}", dt[0]["wuliaobianhao"]), ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "pianchazhongliang", ValueMember = string.Format("{0}", dt[0]["pianchazhongliang"]), ValueType = DataValueCommonType.数字 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "pianchazhuangtai", ValueMember = string.Format("{0}", dt[0]["pianchazhuangtai"]), ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "huansuangongshi", ValueMember = string.Format("{0}", dt[0]["huansuangongshi"]), ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "morenkouzhong", ValueMember = string.Format("{0}", dt[0]["morenkouzhong"]), ValueType = DataValueCommonType.数字 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "kouzhongfangshi", ValueMember = string.Format("{0}", dt[0]["kouzhongfangshi"]), ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = string.Format("{0}", dt[0]["zhandian"]), ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = string.Format("{0}", dt[0]["kehubianhao"]), ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "shuiwenliao", ValueMember = string.Format("{0}", dt[0]["shuiwenliao"]), ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "hunningtu", ValueMember = string.Format("{0}", dt[0]["hunningtu"]), ValueType = DataValueCommonType.字符串 });
                        xingHaoRow.DataCols.Add(new TableCol() { ColName = "liqingliao", ValueMember = string.Format("{0}", dt[0]["liqingliao"]), ValueType = DataValueCommonType.字符串 });
                        if (KeaoUtils._DB.SaveTableRows(xingHaoRow) > 0)
                        {
                            
                        }
                        break;
                    case 0x05:
                        DataTable danWeiDt = KeaoUtils._DB.GetDataTableByCondition("tbl_danwei", "id", null, KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", dt[0]["id"]), "", SearchConditionType.等于), null, null);
                        TableRow danWeiRow = new TableRow() { TableName = "tbl_danwei", ID = string.Format("{0}", dt[0]["id"]) };
                        if (danWeiDt.Rows.Count == 0)
                        {
                            danWeiRow.Ctl = DBCtl.插入;
                        }
                        else
                        {
                            danWeiRow.Ctl = DBCtl.修改;
                        }
                        danWeiRow.DataCols.Add(new TableCol() { ColName = "danweimingcheng", ValueMember = string.Format("{0}", dt[0]["danweimingcheng"]), ValueType = DataValueCommonType.字符串 });
                        danWeiRow.DataCols.Add(new TableCol() { ColName = "danweizhuangtai", ValueMember = string.Format("{0}", dt[0]["danweizhuangtai"]), ValueType = DataValueCommonType.字符串 });
                        danWeiRow.DataCols.Add(new TableCol() { ColName = "danweileixing", ValueMember = string.Format("{0}", dt[0]["danweileixing"]), ValueType = DataValueCommonType.字符串 });
                        danWeiRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        danWeiRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = string.Format("{0}", dt[0]["zhandian"]), ValueType = DataValueCommonType.字符串 });
                        danWeiRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = string.Format("{0}", dt[0]["kehubianhao"]), ValueType = DataValueCommonType.字符串 });
                        danWeiRow.DataCols.Add(new TableCol() { ColName="gongyingshang", ValueMember=string.Format("{0}",dt[0]["gongyingshang"]), ValueType= DataValueCommonType.字符串 });
                        danWeiRow.DataCols.Add(new TableCol() { ColName = "gongyingshangmingcheng", ValueMember = string.Format("{0}", dt[0]["gongyingshangmingcheng"]), ValueType = DataValueCommonType.字符串 });

                        if (KeaoUtils._DB.SaveTableRows(danWeiRow) > 0)
                        {
                            
                        }
                        break;
                    case 0x06:
                        DataTable biaoDuanDt = KeaoUtils._DB.GetDataTableByCondition("tbl_biaoduan", "id", null, KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", dt[0]["id"]), "", SearchConditionType.等于), null, null);
                        TableRow biaoDuanRow = new TableRow() { TableName = "tbl_biaoduan", ID = string.Format("{0}", dt[0]["id"]) };
                        if (biaoDuanDt.Rows.Count == 0)
                        {
                            biaoDuanRow.Ctl = DBCtl.插入;
                        }
                        else
                        {
                            biaoDuanRow.Ctl = DBCtl.修改;
                        }
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "biaoduanmingcheng", ValueMember = string.Format("{0}", dt[0]["biaoduanmingcheng"]), ValueType = DataValueCommonType.字符串 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "biaoduanchangdu", ValueMember = string.Format("{0}", dt[0]["biaoduanchangdu"]), ValueType = DataValueCommonType.数字 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "biaoduankuandu", ValueMember = string.Format("{0}", dt[0]["biaoduankuandu"]), ValueType = DataValueCommonType.数字 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "biaoduanleixing", ValueMember = string.Format("{0}", dt[0]["biaoduanleixing"]), ValueType = DataValueCommonType.字符串 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = string.Format("{0}", dt[0]["zhandian"]), ValueType = DataValueCommonType.字符串 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = string.Format("{0}", dt[0]["kehubianhao"]), ValueType = DataValueCommonType.字符串 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "biaoduanhoudu", ValueMember = string.Format("{0}", dt[0]["biaoduanhoudu"]), ValueType = DataValueCommonType.数字 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "zuoyoufu", ValueMember = string.Format("{0}", dt[0]["zuoyoufu"]), ValueType = DataValueCommonType.字符串 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "gonglihao1", ValueMember = string.Format("{0}", dt[0]["gonglihao1"]), ValueType = DataValueCommonType.数字 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "gonglihao2", ValueMember = string.Format("{0}", dt[0]["gonglihao2"]), ValueType = DataValueCommonType.数字 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "riqi", ValueMember =  dt[0]["riqi"], ValueType = DataValueCommonType.日期时间 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "fangliang", ValueMember = string.Format("{0}", dt[0]["fangliang"]), ValueType = DataValueCommonType.数字 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "ganmidu", ValueMember = string.Format("{0}", dt[0]["ganmidu"]), ValueType = DataValueCommonType.数字 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "gongcheng", ValueMember = string.Format("{0}", dt[0]["gongcheng"]), ValueType = DataValueCommonType.字符串 });
                        biaoDuanRow.DataCols.Add(new TableCol() { ColName = "jihualiang", ValueMember = string.Format("{0}", dt[0]["jihualiang"]), ValueType = DataValueCommonType.数字 });
                        if (KeaoUtils._DB.SaveTableRows(biaoDuanRow) > 0)
                        {
                            
                        }
                        break;
                    case 0x07:
                        DataTable yunShuDt = KeaoUtils._DB.GetDataTableByCondition("tbl_yunshugongsi", "id", null, KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", dt[0]["id"]), "", SearchConditionType.等于), null, null);
                        TableRow yunShuRow = new TableRow() { TableName = "tbl_yunshugongsi", ID = string.Format("{0}", dt[0]["id"]) };
                        if (yunShuDt.Rows.Count == 0)
                        {
                            yunShuRow.Ctl = DBCtl.插入;
                        }
                        else
                        {
                            yunShuRow.Ctl = DBCtl.修改;
                        }
                        yunShuRow.DataCols.Add(new TableCol() { ColName = "gongsimingcheng", ValueMember = string.Format("{0}", dt[0]["gongsimingcheng"]), ValueType = DataValueCommonType.字符串 });
                        yunShuRow.DataCols.Add(new TableCol() { ColName = "gongsizhuangtai", ValueMember = string.Format("{0}", dt[0]["gongsizhuangtai"]), ValueType = DataValueCommonType.字符串 });
                        yunShuRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        yunShuRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = string.Format("{0}", dt[0]["zhandian"]), ValueType = DataValueCommonType.字符串 });
                        yunShuRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = string.Format("{0}", dt[0]["kehubianhao"]), ValueType = DataValueCommonType.字符串 });
                        if (KeaoUtils._DB.SaveTableRows(yunShuRow) > 0)
                        {
                            
                        }
                        break;
                    case 0x19:
                        //合同
                        break;
                    case 0x20:
                        DataTable peiHeBiDt = KeaoUtils._DB.GetDataTableByCondition("tbl_peihebi", KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", dt[0]["id"]), "", SearchConditionType.等于));
                        TableRow peiHeBiRow = new TableRow() { TableName = "tbl_peihebi", ID = string.Format("{0}", dt[0]["id"]) };
                        if (peiHeBiDt.Rows.Count == 0)
                        {
                            peiHeBiRow.Ctl = DBCtl.插入;
                        }
                        else
                        {
                            peiHeBiRow.Ctl = DBCtl.修改;
                        }
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "peibiriqi", ValueMember = string.Format("{0}", dt[0]["peibiriqi"]), ValueType = DataValueCommonType.日期时间 });
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "gongcheng", ValueMember = string.Format("{0}", dt[0]["gongcheng"]), ValueType = DataValueCommonType.字符串 });
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "peibileixing", ValueMember = string.Format("{0}", dt[0]["peibileixing"]), ValueType = DataValueCommonType.字符串 });
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "peibiwuliao", ValueMember = string.Format("{0}", dt[0]["peibiwuliao"]), ValueType = DataValueCommonType.字符串 });
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "guanlianwuliao", ValueMember = string.Format("{0}", dt[0]["guanlianwuliao"]), ValueType = DataValueCommonType.字符串 });
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "guanlianbianhao", ValueMember = string.Format("{0}", dt[0]["guanlianbianhao"]), ValueType = DataValueCommonType.字符串 });
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "peibi", ValueMember = string.Format("{0}", dt[0]["peibi"]), ValueType = DataValueCommonType.数字 });
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = string.Format("{0}", dt[0]["zhandian"]), ValueType = DataValueCommonType.字符串 });
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = string.Format("{0}", dt[0]["kehubianhao"]), ValueType = DataValueCommonType.字符串 });
                        peiHeBiRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        if (KeaoUtils._DB.SaveTableRows(peiHeBiRow) > 0)
                        {

                        }
                        break;
                    case 0x50:
                        DataTable peiBiDanDt = KeaoUtils._DB.GetDataTableByCondition("tbl_peibidan", KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", dt[0]["id"]), "", SearchConditionType.等于));
                        TableRow peiBiDanRow = new TableRow() { TableName="tbl_peibidan", ID=string.Format("{0}",dt[0]["id"]) };
                        if (peiBiDanDt.Rows.Count == 0)
                        {
                            peiBiDanRow.Ctl = DBCtl.插入;
                        }
                        else
                        {
                            peiBiDanRow.Ctl = DBCtl.修改;
                        }
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName="mingcheng", ValueMember=string.Format("{0}",dt[0]["mingcheng"]), ValueType= DataValueCommonType.字符串 });
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName = "leixing", ValueMember = string.Format("{0}", dt[0]["leixing"]), ValueType = DataValueCommonType.字符串 });
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName = "peibiwuliao", ValueMember = string.Format("{0}", dt[0]["peibiwuliao"]), ValueType = DataValueCommonType.字符串 });
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName = "guanlianwuliao", ValueMember = string.Format("{0}", dt[0]["guanlianwuliao"]), ValueType = DataValueCommonType.字符串 });
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName = "guanlianbianhao", ValueMember = string.Format("{0}", dt[0]["guanlianbianhao"]), ValueType = DataValueCommonType.字符串 });
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName = "peibi", ValueMember = string.Format("{0}", dt[0]["peibi"]), ValueType = DataValueCommonType.数字 });
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = string.Format("{0}", dt[0]["zhandian"]), ValueType = DataValueCommonType.字符串 });
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = string.Format("{0}", dt[0]["kehubianhao"]), ValueType = DataValueCommonType.字符串 });
                        peiBiDanRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        if (KeaoUtils._DB.SaveTableRows(peiBiDanRow) > 0)
                        {

                        }
                        break;
                    
                    #endregion
                }
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("处理收到数据异常。", err.Message));
            }
        }

        void m_Socket_InitNetWorkStatus(object sender, EventArgs e)
        {
            //是否连接上网络
            bool link =Convert.ToBoolean(sender);
            if (link)
            {
                status_lb_linkserver.Text = "连接远程服务器成功。";
            }
            else
            {
                status_lb_linkserver.Text = "连接远程服务器失败！";
            }
        }

        private void UpLoadData()
        {
            try
            {
                if (!m_DTU.Init(KeaoUtils.ConfigGetNodeAttr("网络设备", "地址")))
                {
                    return;
                }

                if (!m_DTU.GetNet())
                {
                    return;
                }

                string createCondition = KeaoUtils._DB.CreateAndCondition("shangchuanzhuangtai", DataValueCommonType.字符串, "0", "", SearchConditionType.等于);
                while (true)
                {
                    try
                    {
                        #region 发送表数据
                        DataTable yongHuDt = KeaoUtils._DB.GetDataTableByCondition("tbl_yonghu", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x01, yongHuDt);

                        DataTable jueSeDt = KeaoUtils._DB.GetDataTableByCondition("tbl_juese", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x02, jueSeDt);

                        DataTable wuLiaoDt = KeaoUtils._DB.GetDataTableByCondition("tbl_wuliao", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x03, wuLiaoDt);

                        DataTable xingHaoDt = KeaoUtils._DB.GetDataTableByCondition("tbl_xinghao", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x04, xingHaoDt);

                        DataTable danWeiDt = KeaoUtils._DB.GetDataTableByCondition("tbl_danwei", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x05, danWeiDt);

                        DataTable biaoDuanDt = KeaoUtils._DB.GetDataTableByCondition("tbl_biaoduan", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x06, biaoDuanDt);

                        DataTable yunShuGongSiDt = KeaoUtils._DB.GetDataTableByCondition("tbl_yunshugongsi", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x07, yunShuGongSiDt);

                        DataTable yaoKongDt = KeaoUtils._DB.GetDataTableByCondition("tbl_yaokongjiance", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x08, yaoKongDt);

                        DataTable piZhongDt = KeaoUtils._DB.GetDataTableByCondition("tbl_pizhongyichang", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x09, piZhongDt);

                        DataTable chaoShiDt = KeaoUtils._DB.GetDataTableByCondition("tbl_chaoshi", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x10, chaoShiDt);

                        Dictionary<string, string> orderBys = new Dictionary<string, string>();
                        orderBys.Add("pizhongshijian", "desc");
                        DataTable chengZhongDt = KeaoUtils._DB.GetDataTableByCondition("tbl_chengzhong", "*", null, createCondition, null, orderBys, 1);
                        m_DTU.SendData(0x11, chengZhongDt);

                        DataTable riZhiDt = KeaoUtils._DB.GetDataTableByCondition("tbl_rizhi", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x14, riZhiDt);

                        DataTable tuiKuDt = KeaoUtils._DB.GetDataTableByCondition("tbl_tuiku", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x15, tuiKuDt);

                        DataTable gongYingShangDt = KeaoUtils._DB.GetDataTableByCondition("tbl_gongyingshang", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x18, gongYingShangDt);

                        DataTable peiHeBiDt = KeaoUtils._DB.GetDataTableByCondition("tbl_peihebi", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x20, peiHeBiDt);

                        DataTable peiBiDanDt = KeaoUtils._DB.GetDataTableByCondition("tbl_peibidan", "*", null, createCondition, null, null, 1);
                        m_DTU.SendData(0x50, peiBiDanDt);
                        #endregion

                        #region 发送图片
                        if (chengZhongDt.Rows.Count > 0&&KeaoUtils.ConfigGetNodeAttr("网络设备","上传图片")=="启用")
                        {
                            string maoZhongTuPian1 = string.Format("{0}", chengZhongDt.Rows[0]["maozhongtupian1"]);
                            if (!string.IsNullOrEmpty(maoZhongTuPian1))
                            {
                                m_DTU.SendImage(maoZhongTuPian1);
                                Thread.Sleep(30000);
                                m_DTU.SendEndByte();
                            }
                            else
                            {
                                Thread.Sleep(10000);
                            }
                            string maoZhongTuPian2 = string.Format("{0}", chengZhongDt.Rows[0]["maozhongtupian2"]);
                            if (!string.IsNullOrEmpty(maoZhongTuPian2))
                            {
                                m_DTU.SendImage(maoZhongTuPian2);
                                Thread.Sleep(30000);
                                m_DTU.SendEndByte();
                            }
                            else
                            {
                                Thread.Sleep(10000);
                            }

                            string maoZhongTuPian3 = string.Format("{0}", chengZhongDt.Rows[0]["maozhongtupian3"]);
                            if (!string.IsNullOrEmpty(maoZhongTuPian3))
                            {
                                m_DTU.SendImage(maoZhongTuPian3);
                                Thread.Sleep(30000);
                                m_DTU.SendEndByte();
                            }
                            else
                            {
                                Thread.Sleep(10000);
                            }

                            string piZhongTuPian1 = string.Format("{0}", chengZhongDt.Rows[0]["pizhongtupian1"]);
                            if (!string.IsNullOrEmpty(piZhongTuPian1))
                            {
                                m_DTU.SendImage(piZhongTuPian1);
                                Thread.Sleep(30000);
                                m_DTU.SendEndByte();
                            }
                            else
                            {
                                Thread.Sleep(10000);
                            }

                            string piZhongTuPian2 = string.Format("{0}", chengZhongDt.Rows[0]["pizhongtupian2"]);
                            if (!string.IsNullOrEmpty(piZhongTuPian2))
                            {
                                m_DTU.SendImage(piZhongTuPian2);
                                Thread.Sleep(30000);
                                m_DTU.SendEndByte();
                            }
                            else
                            {
                                Thread.Sleep(10000);
                            }

                            string piZhongTuPian3 = string.Format("{0}", chengZhongDt.Rows[0]["pizhongtupian3"]);
                            if (!string.IsNullOrEmpty(piZhongTuPian3))
                            {
                                m_DTU.SendImage(piZhongTuPian3);
                                Thread.Sleep(30000);
                                m_DTU.SendEndByte();
                            }
                            else
                            {
                                Thread.Sleep(10000);
                            }
                        }
                        else
                        {
                            //休息1分钟
                            Thread.Sleep(30000);
                            menu_1_shujuyun_tongbu_Click(null, null);
                            Thread.Sleep(30000);
                        }
                        #endregion

                        if (KeaoUtils.ConfigGetNodeAttr("网络设备", "类型") == "KeaoDTU")
                        {
                            #region 短信提醒

                            DateTime nowDate = DateTime.Now;
                            if (nowDate.Hour >= int.Parse(KeaoUtils.ConfigGetNodeAttr("时段", "提醒时间")) && KeaoUtils.ConfigGetNodeAttr("网络设备", "短信发送日期") != nowDate.ToString("yyyy-MM-dd"))
                            {
                                KeaoUtils.ConfigSetNodeAttr("网络设备", "短信发送日期", nowDate.ToString("yyyy-MM-dd"));
                                string sendStr = string.Empty;
                                DateTime kaiShi = DateTime.Today.AddDays(-1);
                                DateTime jieShu = kaiShi.AddHours(23).AddMinutes(59).AddSeconds(59);
                                string ruKuCondition = KeaoUtils._DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), jieShu.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                                ruKuCondition += KeaoUtils._DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, "入库物料", "", SearchConditionType.等于);
                                DataTable ruKuDt = KeaoUtils._DB.GetDataTableByCondition("tbl_chengzhong", "wuliaomingcheng,xinghao,count(id) as cheshu,sum(jingzhong) as jingzhong", null, ruKuCondition, "wuliaomingcheng,xinghao", null);
                                if (ruKuDt.Rows.Count > 0)
                                {
                                    sendStr += string.Format("[{0}_{1}_入库]\n", KeaoUtils.ConfigGetNodeAttr("客户", "站点"), KeaoUtils.ConfigGetNodeAttr("客户", "磅房"));
                                    foreach (DataRow row in ruKuDt.Rows)
                                    {
                                        sendStr += string.Format("{0},{1},{2}车,{3}吨\n", row["wuliaomingcheng"], row["xinghao"], row["cheshu"], row["jingzhong"]);
                                    }
                                }
                                else
                                {
                                    sendStr += string.Format("[{0}_{1}_入库]没有称重数据\n", KeaoUtils.ConfigGetNodeAttr("客户", "站点"), KeaoUtils.ConfigGetNodeAttr("客户", "磅房"));
                                }
                                string chuKuCondition = KeaoUtils._DB.CreateAndCondition("maozhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), jieShu.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                                chuKuCondition += KeaoUtils._DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, "出库物料", "", SearchConditionType.等于);
                                DataTable chuKuDt = KeaoUtils._DB.GetDataTableByCondition("tbl_chengzhong", "wuliaomingcheng,xinghao,count(id) as cheshu,sum(jingzhong) as jingzhong", null, chuKuCondition, "wuliaomingcheng,xinghao", null);
                                if (chuKuDt.Rows.Count > 0)
                                {
                                    sendStr += string.Format("[{0}_{1}_出库]\n", KeaoUtils.ConfigGetNodeAttr("客户", "站点"), KeaoUtils.ConfigGetNodeAttr("客户", "磅房"));
                                    foreach (DataRow row in chuKuDt.Rows)
                                    {
                                        sendStr += string.Format("{0},{1},{2}车,{3}吨\n", row["wuliaomingcheng"], row["xinghao"], row["cheshu"], row["jingzhong"]);
                                    }
                                }
                                else
                                {
                                    sendStr += string.Format("[{0}_{1}_出库]没有称重数据\n", KeaoUtils.ConfigGetNodeAttr("客户", "站点"), KeaoUtils.ConfigGetNodeAttr("客户", "磅房"));
                                }
                                if (!string.IsNullOrEmpty(sendStr))
                                {

                                    if (sendStr.Length <= 145)
                                    {
                                        XmlNodeList nodeList = KeaoUtils.ConfigGetNodeListByTag("发送号码");
                                        foreach (XmlNode node in nodeList)
                                        {
                                            m_DTU.SendSMS(sendStr, node.InnerText);
                                        }
                                    }
                                    else
                                    {
                                        XmlNodeList nodeList = KeaoUtils.ConfigGetNodeListByTag("发送号码");
                                        for (int i = 0; i < sendStr.Length; i += 145)
                                        {
                                            if (i + 145 > sendStr.Length)
                                            {
                                                foreach (XmlNode node in nodeList)
                                                {
                                                    m_DTU.SendSMS(sendStr.Substring(i), node.InnerText);
                                                }
                                                break;
                                            }

                                            foreach (XmlNode node in nodeList)
                                            {
                                                m_DTU.SendSMS(sendStr.Substring(i, 145), node.InnerText);
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                        
                    }
                    catch (ThreadAbortException)
                    {
                        return;
                    }
                    catch (Exception err)
                    {
                        KeaoUtils.LogTxt(string.Format("上传数据异常。{0}", err.Message));
                        Thread.Sleep(300000);
                    }
                }
            }
            catch (ThreadAbortException)
            {
                return;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("启动上传线程异常。{0}", err.Message));
            }
        }


        private void Bak(object Args)
        {
            try
            {
                string bakStr = KeaoUtils.ConfigGetNodeAttr("数据备份", "备份路径");
                DateTime nowDate = DateTime.Now;
                if (!Directory.Exists("bakStr"))
                {
                    Directory.CreateDirectory(bakStr);
                }
                File.Copy("keaodb.dat", string.Format("{1}\\keaodb{0}.dat", nowDate.ToString("yyyyMMddHHmm"),bakStr));
                DirectoryInfo dirInfo = new DirectoryInfo("Bak");
                FileInfo[] fileInfos = dirInfo.GetFiles();
                foreach (FileInfo f in fileInfos)
                {
                    if ((nowDate - f.CreationTime).TotalDays > 30.0)
                    {
                        f.Delete();
                    }
                }
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("备份数据库异常。{0}", err.Message));
            }
        }

        private void menu_1_xitong_tuichu_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menu_1_xitong_xitongshezhi_Click(object sender, EventArgs e)
        {
            //系统设置
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("系统配置"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            SysConfig sysConfig = new SysConfig();
            sysConfig.ShowDialog();
        }

        private void menu_1_xitong_xiugaimima_Click(object sender, EventArgs e)
        {
            XiuGaiMiMa xiuGaiMiMa = new XiuGaiMiMa();
            if (xiuGaiMiMa.ShowDialog() == DialogResult.OK)
            {
                KeaoUtils.ShowInfoMessage("密码修改成功，下次登录生效。");
            }
        }

        private void menu_1_xinxi_juese_Click(object sender, EventArgs e)
        {
            //角色管理
            JueSeGuanLi jueSeGuanLi = new JueSeGuanLi();
            jueSeGuanLi.ShowDialog();
        }

        private void menu_1_xinxi_yonghu_Click(object sender, EventArgs e)
        {
            //用户管理
            YongHuGuanLi yongHuGuanLi = new YongHuGuanLi();
            yongHuGuanLi.ShowDialog();
        }

        private void menu_1_xinxi_rukuwuliao_Click(object sender, EventArgs e)
        {
            //入库物料管理
            WuLiaoGuanLi wuLiaoGuanLi = new WuLiaoGuanLi(true);
            wuLiaoGuanLi.ShowDialog();
        }

        private void menu_1_xinxi_wuliao_chuku_Click(object sender, EventArgs e)
        {
            //出库物料管理
            WuLiaoGuanLi wuLiaoGuanLi = new WuLiaoGuanLi(false);
            wuLiaoGuanLi.ShowDialog();
        }

        private void menu_1_xinxi_rukucheliang_Click(object sender, EventArgs e)
        {
            //入库车辆管理
            CheLiangGuanLi cheLiangGuanLi = new CheLiangGuanLi(true);
            cheLiangGuanLi.ShowDialog();
        }

        private void menu_1_xinxi_cheliang_chuku_Click(object sender, EventArgs e)
        {
            //出库车辆管理
            CheLiangGuanLi cheLiangGuanLi = new CheLiangGuanLi(false);
            cheLiangGuanLi.ShowDialog();
        }

        private void menu_1_xinxi_danwei_ruku_Click(object sender, EventArgs e)
        {
            //入库单位管理
            DanWeiGuanLi danWeiGuanLi = new DanWeiGuanLi(true);
            danWeiGuanLi.ShowDialog();
        }

        private void menu_1_xinxi_danwei_chuku_Click(object sender, EventArgs e)
        {
            //出库单位管理
            DanWeiGuanLi danWeiGuanLi = new DanWeiGuanLi(false);
            danWeiGuanLi.ShowDialog();
        }

        private void menu_1_xinxi_wuziquxiang_Click(object sender, EventArgs e)
        {
            //去向管理
            BiaoDuanGuanLi biaoDuanGuanLi = new BiaoDuanGuanLi();
            biaoDuanGuanLi.ShowDialog();
        }

        private void menu_1_chengzhong_bupiao_Click(object sender, EventArgs e)
        {
            //入库补票录入
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("入库补票"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            YiWanChengXiuGai buPiaoLuRu = new YiWanChengXiuGai(null, "入库物料");
            if (buPiaoLuRu.ShowDialog() == DialogResult.OK)
            {
                BindRiJieRuKu();
                BindCheLiangYunShuCiShu();
            }
        }

        private void menu_1_chengzhong_chukubupiao_Click(object sender, EventArgs e)
        {
            //出库补票录入
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("出库补票"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            YiWanChengXiuGai buPiaoLuRu = new YiWanChengXiuGai(null, "出库物料");
            if (buPiaoLuRu.ShowDialog() == DialogResult.OK)
            {
                BindRiJieChuKu();
                BindCheLiangYunShuCiShu();
            }
        }

        /// <summary>
        /// 称重系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menu_1_chengzhong_chengzhong_Click(object sender, EventArgs e)
        {
            ChengZhong chengZhong = new ChengZhong();
            chengZhong.Show();
            chengZhong.CloseEvent += new EventHandler(chengZhong_CloseEvent);
            chengZhong.OnChengZhong += new EventHandler(chengZhong_OnChengZhong);
            chengZhong.OnChengZhongChaoCha += new EventHandler(chengZhong_OnChengZhongChaoCha);
            chengZhong.OnChengZhongChaoShi += new EventHandler(chengZhong_OnChengZhongChaoShi);
            menu_1_chengzhong_chengzhong.Enabled = false;
        }

        void chengZhong_OnChengZhongChaoShi(object sender, EventArgs e)
        {
            //超时
            BindChaoShi();
        }

        void chengZhong_OnChengZhongChaoCha(object sender, EventArgs e)
        {
            //超差
            BindPiZhongChaoCha();
        }

        void chengZhong_OnChengZhong(object sender, EventArgs e)
        {
            //新称重信息
            BindCheLiangYunShuCiShu();
            BindRiJieChuKu();
            BindRiJieRuKu();
            BindChaoShi();
            BindPiZhongChaoCha();
        }

        void chengZhong_CloseEvent(object sender, EventArgs e)
        {
            menu_1_chengzhong_chengzhong.Enabled = true;
        }

        //运输公司管理
        private void menu_1_xinxi_yunshugongsi_Click(object sender, EventArgs e)
        {
            YunShuGongSiGuanLi yunGuanLi = new YunShuGongSiGuanLi();
            yunGuanLi.ShowDialog();
        }

        private void BindPiZhongChaoCha()
        {
            try
            {
                DateTime kaiShi = DateTime.Parse(string.Format("{0} 00:00:00", DateTime.Today.ToString("yyyy-MM-dd")));
                string createCondition = KeaoUtils._DB.CreateAndCondition("chengzhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), kaiShi.AddHours(23).AddMinutes(59).AddSeconds(59).ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_pizhongyichang", createCondition);
                dataGridView3.DataSource = Dt;
            }
            catch (Exception err)
            {
               KeaoUtils.LogTxt(string.Format("绑定今日皮重超差数据异常。{0}",err.Message));
            }
        }


        private void BindChaoShi()
        {
            try
            {
                DateTime kaiShi = DateTime.Parse(string.Format("{0} 00:00:00", DateTime.Today.ToString("yyyy-MM-dd")));
                string createCondition = KeaoUtils._DB.CreateAndCondition("jinchangshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), kaiShi.AddHours(23).AddMinutes(59).AddSeconds(59).ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_chaoshi", createCondition);
                dataGridView7.DataSource = Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定今日超时信息异常。{0}", err.Message));
            }
        }

        public static object m_CiShuDt;

        private void BindCheLiangYunShuCiShu()
        {
            try
            {
                DateTime kaiShi = DateTime.Parse(string.Format("{0} 00:00:00", DateTime.Today.ToString("yyyy-MM-dd")));
                string createCondition = KeaoUtils._DB.CreateAndCondition("maozhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), kaiShi.AddHours(23).AddMinutes(59).AddSeconds(59).ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_chengzhong", "chepaihao,chengzhongleixing,count(id) as cishu", null, createCondition, "chepaihao,chengzhongleixing", null);
                dataGridView8.DataSource = Dt;
                m_CiShuDt = dataGridView8.DataSource;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定今日运输次数异常。{0}", err.Message));
            }
        }

        private void BindRiJieRuKu()
        {
            try
            {
                DateTime kaiShi = DateTime.Parse(string.Format("{0} 00:00:00", DateTime.Today.ToString("yyyy-MM-dd")));
                string createCondition = KeaoUtils._DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), kaiShi.AddHours(23).AddMinutes(59).AddSeconds(59).ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                createCondition += KeaoUtils._DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, "入库物料", "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_chengzhong", "wuliaomingcheng,xinghao,count(id) as hejicheshu,sum(jingzhong) as hejijingzhong", null, createCondition, "wuliaomingcheng,xinghao", null);
                dataGridView1.DataSource = Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定日结入库数据异常。{0}", err.Message));
            }
        }

        private void BindRiJieChuKu()
        {
            try
            {
                DateTime kaiShi = DateTime.Parse(string.Format("{0} 00:00:00", DateTime.Today.ToString("yyyy-MM-dd")));
                string createCondition = KeaoUtils._DB.CreateAndCondition("maozhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), kaiShi.AddHours(23).AddMinutes(59).AddSeconds(59).ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                createCondition += KeaoUtils._DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, "出库物料", "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_chengzhong", "wuliaomingcheng,xinghao,count(id) as hejicheshu,sum(jingzhong) as hejijingzhong", null, createCondition, "wuliaomingcheng,xinghao", null);
                dataGridView2.DataSource = Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定日结出库数据异常。{0}", err.Message));
            }
        }

        private void menu_1_chaxun_mingxi_ruku_Click(object sender, EventArgs e)
        {
            //入库明细
            ChengZhongMingXi mingXi = new ChengZhongMingXi("入库物料");
            mingXi.OnMingXiClose += new EventHandler(mingXi_OnMingXiClose);
            mingXi.Show();
        }

        void mingXi_OnMingXiClose(object sender, EventArgs e)
        {
            BindRiJieChuKu();
            BindRiJieRuKu();
            BindCheLiangYunShuCiShu();
        }

        private void menu_1_chaxun_mingxi_chuku_Click(object sender, EventArgs e)
        {
             //出库明细
            ChengZhongMingXi mingXi = new ChengZhongMingXi("出库物料");
            mingXi.OnMingXiClose+=new EventHandler(mingXi_OnMingXiClose);
            mingXi.Show();
        }

        private void menu_1_jiankong_Click(object sender, EventArgs e)
        {
            ShiPinJianKong shiPin = new ShiPinJianKong();
            shiPin.OnClose += new EventHandler(shiPin_OnClose);
            shiPin.Show();
            menu_1_jiankong.Enabled = false;
        }

        void shiPin_OnClose(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            menu_1_jiankong.Enabled = true;
        }

        private void menu_1_chaxun_pizhongchaocha_Click(object sender, EventArgs e)
        {
            //皮重超差查询
            PiZhongChaoCha piZhongChaoCha = new PiZhongChaoCha();
            piZhongChaoCha.Show();
        }

        private void menu_1_chaxun_cheliangchaoshi_Click(object sender, EventArgs e)
        {
            //车辆停留超时查询
            CheLiangChaoShi cheLiangChaoShi = new CheLiangChaoShi();
            cheLiangChaoShi.Show();
        }

        private void menu_1_chaxun_riqi_ruku_Click(object sender, EventArgs e)
        {
            //入库日期汇总查询
            RiQiHuiZong huiZong = new RiQiHuiZong("入库物料");
            huiZong.Show();
        }

        private void menu_1_chaxun_riqi_chuku_Click(object sender, EventArgs e)
        {
            //出库日期汇总查询
            RiQiHuiZong huiZong = new RiQiHuiZong("出库物料");
            huiZong.Show();

        }

        private void menu_1_chaxun_danwei_ruku_Click(object sender, EventArgs e)
        {
            //入库单位汇总
            DanWeiHuiZong danWeiHuiZong = new DanWeiHuiZong("入库物料");
            danWeiHuiZong.Show();
        }

        private void menu_1_chaxun_danwei_chuku_Click(object sender, EventArgs e)
        {
            //出库单位汇总
            DanWeiHuiZong danWeiHuiZong = new DanWeiHuiZong("出库物料");
            danWeiHuiZong.Show();
        }

        private void menu_1_chaxun_cheliangyunshu_Click(object sender, EventArgs e)
        {
            //车辆运输次数查询
            CheLiangYunShuCiShu cheLiangYunShu = new CheLiangYunShuCiShu();
            cheLiangYunShu.Show();
        }

        private void menu_1_chaxun_wuliao_ruku_Click(object sender, EventArgs e)
        {
            //入库物料汇总
            WuLiaoHuiZong wuLiao = new WuLiaoHuiZong("入库物料");
            wuLiao.Show();
        }

        private void menu_1_chaxun_wuliao_chuku_Click(object sender, EventArgs e)
        {
            //出库物料汇总
            WuLiaoHuiZong wuLiao = new WuLiaoHuiZong("出库物料");
            wuLiao.Show();
        }

        private void menu_1_chengzhong_yaokong_Click(object sender, EventArgs e)
        {
            //遥控监测
            YaoKongJianCeJieGuo yaoKong = new YaoKongJianCeJieGuo();
            yaoKong.Show();
        }

        private void menu_1_chaxun_kucun_Click(object sender, EventArgs e)
        {
            //仓库盘存
            CangKuPanCun cangKuPanCun = new CangKuPanCun();
            cangKuPanCun.ShowDialog();
        }

        private void menu_1_chaxun_baobiao_rukurijie_Click(object sender, EventArgs e)
        {
            //入库日结报表
            RiJieBaoBiao riJie = new RiJieBaoBiao("入库物料");
            riJie.ShowDialog();
        }

        private void menu_1_chaxun_baobiao_rukuyuejie_Click(object sender, EventArgs e)
        {
            //入库月结报表
            YueJieBaoBiao yueJie = new YueJieBaoBiao("入库物料");
            yueJie.ShowDialog();
        }

        private void menu_1_chaxun_baobiao_chukurijie_Click(object sender, EventArgs e)
        {
            //出库日结报表
            RiJieBaoBiao riJie = new RiJieBaoBiao("出库物料");
            riJie.ShowDialog();
        }

        private void menu_1_chaxun_baobiao_chukuyuejie_Click(object sender, EventArgs e)
        {
            //出库月结报表
            YueJieBaoBiao yueJie = new YueJieBaoBiao("出库物料");
            yueJie.ShowDialog();
        }


        private void menu_1_shujuyun_tongbu_Click(object sender, EventArgs e)
        {
            //下载服务端信息

            if (sender == null)
            {
                //上传客户端信息
                m_DTU.SendData(0x42, m_ClientDt);
            }
            else
            {
                m_DTU.SendData(0x41, m_ClientDt);
                KeaoUtils.ShowInfoMessage("数据获取命令已发出。");
            }
        }

        private void menu_1_chaxun_rizhi_Click(object sender, EventArgs e)
        {
            RiZhi riZhi = new RiZhi();
            riZhi.ShowDialog();
        }

        private void menu_1_chengzhong_tuiku_Click(object sender, EventArgs e)
        {
            //称重退库
            if (!KeaoUtils._CurrentUserRow["juesequanxian"].ToString().Contains("退库操作"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有权限进行此操作。");
                return;
            }
            ChengZhongTuiKu chengZhongTuiKu = new ChengZhongTuiKu();
            chengZhongTuiKu.ShowDialog();
        }

        private void menu_1_tuikumingxi_ruku_Click(object sender, EventArgs e)
        {
            //入库物资退库明细
            TuiKuMingXi tuiKuMingXi = new TuiKuMingXi("入库物料");
            tuiKuMingXi.ShowDialog();
        }

        private void menu_1_tuikumingxi_chuku_Click(object sender, EventArgs e)
        {
            //出库物资退库明细
            TuiKuMingXi tuiKuMingXi = new TuiKuMingXi("出库物料");
            tuiKuMingXi.ShowDialog();
        }

        private void menu_1_tuikuriqi_ruku_Click(object sender, EventArgs e)
        {
            //入库物资退库日期汇总
            TuiKuRiQiHuiZong tuiKuRiQiHuiZong = new TuiKuRiQiHuiZong("入库物料");
            tuiKuRiQiHuiZong.ShowDialog();
        }

        private void menu_1_tuikuriqi_chuku_Click(object sender, EventArgs e)
        {
            //出库物资退库日期汇总
            TuiKuRiQiHuiZong tuiKuRiQiHuiZong = new TuiKuRiQiHuiZong("出库物料");
            tuiKuRiQiHuiZong.ShowDialog();
        }

        private void menu_1_tuikudanwei_ruku_Click(object sender, EventArgs e)
        {
            //入库物资退库单位汇总
            TuiKuDanWeiHuiZong tuiKuDanWeiHuiZong = new TuiKuDanWeiHuiZong("入库物料");
            tuiKuDanWeiHuiZong.ShowDialog();
        }

        private void menu_1_tuikudanwei_chuku_Click(object sender, EventArgs e)
        {
            //出库物资退库单位汇总
            TuiKuDanWeiHuiZong tuiKuDanWeiHuiZong = new TuiKuDanWeiHuiZong("出库物料");
            tuiKuDanWeiHuiZong.ShowDialog();
        }

        private void menu_1_tuikuwuliao_ruku_Click(object sender, EventArgs e)
        {
            //入库物资退库物料汇总
            TuiKuWuLiaoHuiZong tuiKuWuLiaoHuiZong = new TuiKuWuLiaoHuiZong("入库物料");
            tuiKuWuLiaoHuiZong.ShowDialog();
        }

        private void menu_1_tuikuwuliao_chuku_Click(object sender, EventArgs e)
        {
            //出库物资退库物料汇总
            TuiKuWuLiaoHuiZong tuiKuWuLiaoHuiZong = new TuiKuWuLiaoHuiZong("出库物料");
            tuiKuWuLiaoHuiZong.ShowDialog();
        }

        private void menu_1_xinxi_zidian_Click(object sender, EventArgs e)
        {
            //数据字典管理
            ZiDian ziDian = new ZiDian();
            ziDian.ShowDialog();
        }

        private void menu_1_xinxi_peihebi_Click(object sender, EventArgs e)
        {
            //配合比管理
            PeiBiGuanLi peiBiGuanLi = new PeiBiGuanLi();
            peiBiGuanLi.ShowDialog();
        }

        private void menu_1_xinxi_gongyingshang_Click(object sender, EventArgs e)
        {
            GongYingShangGuanLi gys = new GongYingShangGuanLi();
            gys.ShowDialog();
        }

        private void menu_1_xinxi_peibidan_Click(object sender, EventArgs e)
        {
            //配比单管理
            PeiBiDanGuanLi peibidan = new PeiBiDanGuanLi();
            peibidan.ShowDialog();
        }

        private void menu_1_shujuyun_shigong_Click(object sender, EventArgs e)
        {
            //下载施工信息
            m_DTU.SendData(0x43, m_ClientDt);
            KeaoUtils.ShowInfoMessage("数据获取命令已发出。");
        }
    }
}
